﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using hr_1.Model;

namespace hr_1.View
{
    /// <summary>
    /// Interaction logic for Show_employee.xaml
    /// </summary>
    public partial class Show_employee : Page
    {
        public HRDataContext hrData = new HRDataContext();
        public List<Model.PersonPosition> personPositions;

        public Show_employee()
        {
            InitializeComponent();
            var queryPerson = from personInf in hrData.persons
                               select new
                               {
                                   personInf.personId,
                                   personInf.firstname,
                                   personInf.lastname,
                                   personInf.work_activityId
                               };
            personPositions = new List<PersonPosition>();
            foreach (var personInf in queryPerson)
            {
                var newPersonPosition = new PersonPosition
                    {
                        PersonId = personInf.personId,
                        Firstname = personInf.firstname,
                        Lastname = personInf.lastname
                    };
                var queryPosition = from positionInf in hrData.work_activities
                                    where personInf.work_activityId == positionInf.work_activityId
                                    select new
                                    {
                                        positionInf.department,
                                        positionInf.position
                                    };
                foreach (var positionInf in queryPosition)
                {
                    newPersonPosition.Department = positionInf.department;
                    newPersonPosition.Position = positionInf.position;
                }
                personPositions.Add(newPersonPosition);
            }
            dataGrid_show_emp.ItemsSource = personPositions;     
        }

        private void Edit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var temp = (PersonPosition) dataGrid_show_emp.SelectedItem;
                Edit_employee page = new Edit_employee(temp.PersonId);
                NavigationService.Navigate(page);
            }
            catch (InvalidCastException)
            {
                MessageBox.Show("Не выбран сотрудник для редактирования.");
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("Не выбран сотрудник для редактирования.");
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }
    }
}
